package sde.actor.eratostene;
import sde.actor.*;

public class Generatore extends Actor{
	private class Generate extends Message{}
	private int n;
	private int contatore=2;
	private FiltroIF out;
	private Generate generate=new Generate();
	private final byte GENERATING=0, TERMINATED=1;
	public Generatore( int n, FiltroIF out ){
	   this.n=n; this.out=out;
	   send( generate ); //pro-activity � send to itself
	   become( GENERATING );
	}
	public void handler( Message m ){
		   switch( currentStatus() ){
			   case GENERATING:
		   		if( m instanceof Generate ){
	      			    if( contatore==n ){
					out.send( new FiltroIF.Number( 0 ) );
					become( TERMINATED );
				    }
	      			    else{
					contatore++;
	      			            out.send( new FiltroIF.Number( contatore ) );
					send( m ); //pro-activity
			                }
				} break;
			  case TERMINATED:
		   }//switch
	}//handler
}//Generatore

